/*
    Copyright (c);2019-2022;Wiscom System;

	All rights reserved.

    Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

    1. Redistributions of source code must retain the above copyright notice, this
       list of conditions and the following disclaimer.
    2. Redistributions in binary form must reproduce the above copyright notice,
       this list of conditions and the following disclaimer in the documentation
       and/or other materials provided with the distribution.
    3. Neither the name of the Wiscom System nor the names of its contributors
       may be used to endorse or promote products derived from this software without
       specific prior written permission.

    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
OF SUCH DAMAGE.
*/

#include "los_event.h"
#include "los_task.h"
#include "los_api_event.h"
#include "los_inspect_entry.h"
#include "csg_sdk_hal.h"
#ifdef LOSCFG_LIB_LIBC
#include "string.h"
#endif

#ifdef __cplusplus
#if __cplusplus
extern "C" {
#endif /* __cpluscplus */
#endif /* __cpluscplus */

extern UINT32 uwTask1, uwTask2, uwTask3;
/* event control struct */
extern EVENT_CB_S  example_event;
static UINT32 Temp_data=0;
static UINT8 Task_finish_flag=0;
/* wait event type */
#define event_wait 0x00000003

static VOID recv1_Event(VOID)
{
    UINT32 uwEvent;
    UINT32 uwRet = LOS_OK;

    while(Task_finish_flag<1)
    {
	    /*
	     * timeout, WAITMODE to read event, timeout is 100 ticks,
	     * if timeout, wake task directly
	     */
	    dprintf("\r\n3. task2 read event,wait EventMask 0x%x,mode is LOS_WAITMODE_OR. \r\n",event_wait);

	    uwEvent = LOS_EventRead(&example_event, event_wait, LOS_WAITMODE_OR, LOS_WAIT_FOREVER);
	    if(uwEvent==LOS_ERRNO_EVENT_NOT_INITIALIZED)
	    {
	        dprintf("5. task2 read event failed, err is LOS_ERRNO_EVENT_NOT_INITIALIZED. \r\n");
	    }
        else
        {
            dprintf("5. task2 read event success,read EventMask :0x%x . \r\n",uwEvent);
        }
        LOS_TaskDelay(1000);
    }
}

static VOID recv2_Event(VOID)
{
    UINT32 uwEvent;
    UINT32 uwRet = LOS_OK;

    while(Task_finish_flag<1)
    {
	    /*
	     * timeout, WAITMODE to read event, timeout is 100 ticks,
	     * if timeout, wake task directly
	     */
	    dprintf("\r\n3. task3 read event,wait EventMask 0x%x,mode is LOS_WAITMODE_OR \r\n",event_wait);

	    uwEvent = LOS_EventRead(&example_event, event_wait, LOS_WAITMODE_OR, LOS_WAIT_FOREVER);
	    if(uwEvent==LOS_ERRNO_EVENT_NOT_INITIALIZED)
	    {
	        dprintf("5. task3 read failed, err is LOS_ERRNO_EVENT_NOT_INITIALIZED. \r\n");
	    }
        else
        {
            dprintf("5. task3 read event success,read EventMask :0x%x . \r\n",uwEvent);
        }
        LOS_TaskDelay(1000);
    }
}


static VOID send_Event(VOID)
{
    UINT32 uwEvent;
    UINT32 uwRet = LOS_OK;
    UINT32 TempData1=0;
	LOS_TaskDelay(100);

    uwEvent = 0x03;
    /* write event */
    dprintf("4. task1 Write expect EventMask 0x%x .\r\n",uwEvent);
    uwRet = LOS_EventWrite(&example_event, uwEvent);
    if(uwRet != LOS_OK)
    {
        dprintf("\r\nevent write failed .\r\n");
        return LOS_NOK;
    }
    while(TempData1!=0xAA)
    {
        dprintf("4. send 0xAA to destory event .\r\n",uwEvent);
        TempData1=(USART0->RXDATA);
        if(TempData1& USART_RXDATA_EMPTY)
        {
            TempData1=0;
        }
        else
        {
            TempData1=(TempData1 & 0xFF);
        }
        LOS_TaskDelay(1000);
    }
    dprintf("4. task1 destroy mask .\r\n",uwEvent);
    uwRet = LOS_EventDestroy(&example_event);
    if(uwRet != LOS_OK)
    {
        dprintf("\r\nevent Destroy failed .\r\n");
        return LOS_NOK;
    }
    else
    {
        dprintf("\r\nevent Destroy success .\r\n");
    }
    LOS_TaskDelay(2000);
    Task_finish_flag++;
    dprintf("5. Event management event destory test success! \r\n");
    LOS_TaskDelete(uwTask1);
}



UINT32 Example_Event_destroy(VOID)
{
    UINT32 uwRet;
    TSK_INIT_PARAM_S stInitParam1;
	
    dprintf("\r\n--------4.10 Event management event destory test!--------\r\n");
	
    LOS_TaskLock();//lock task schedue
    LOS_TaskDelete(uwTask1);
	LOS_TaskDelete(uwTask2);
    LOS_TaskDelete(uwTask3);

	
    /* event init */
    uwRet = LOS_EventInit(&example_event);
    if(uwRet != LOS_OK)
    {
        dprintf("init event failed .\r\n");
        return LOS_NOK;
    }
    dprintf("\r\n1. Initialize event control block success!\r\n");
    dprintf("\r\n2. Create Example_Event to read event!\r\n");


    /* create task1 */
    stInitParam1.pfnTaskEntry = send_Event;
    stInitParam1.usTaskPrio = 1;
    stInitParam1.uwStackSize = LOSCFG_BASE_CORE_TSK_DEFAULT_STACK_SIZE;
    stInitParam1.pcName = "sendevent";
	
    
    uwRet = LOS_TaskCreate(&uwTask1, &stInitParam1);
    if (uwRet != LOS_OK)
    {
        dprintf("create task1 failed!,error:%x \r\n",uwRet);
        return uwRet;
    }
    dprintf("\r\n1. Create task1 to send Event!\r\n");
	
	/* create task2 */
    stInitParam1.pfnTaskEntry = recv1_Event;
    stInitParam1.usTaskPrio = 2;
    stInitParam1.uwStackSize = LOSCFG_BASE_CORE_TSK_DEFAULT_STACK_SIZE;
    stInitParam1.pcName = "recvevent1";
	
    
    uwRet = LOS_TaskCreate(&uwTask2, &stInitParam1);
    if (uwRet != LOS_OK)
    {
        dprintf("create task2 failed!,error:%x \r\n",uwRet);
        return uwRet;
    }
    dprintf("\r\n1. Create task2 to recv Evnet!\r\n");
	
    /*create task3*/	
    stInitParam1.usTaskPrio = 3;
    stInitParam1.pfnTaskEntry = recv2_Event;	
    stInitParam1.pcName = "recvevent2";
    uwRet = LOS_TaskCreate(&uwTask3, &stInitParam1);
    if (uwRet != LOS_OK)
    {
        dprintf("create task3 failed!,error:%x \r\n",uwRet);
        return uwRet;
    }
    dprintf("\r\n2. Create task3 to receive Event!\r\n");
	
    LOS_TaskUnlock();//unlock task schedue
    while(Task_finish_flag<1)
    {
        LOS_TaskDelay(200);
    }
    Task_finish_flag=0;
    return LOS_OK;
}

#ifdef __cplusplus
#if __cplusplus
}
#endif /* __cpluscplus */
#endif /* __cpluscplus */
